package com.young.ums.web;

import com.young.common.core.excel.ExcelExport;
import com.young.common.core.excel.ExcelImport;
import com.young.common.core.excel.IExcelExport;
import com.young.common.core.excel.IExcelImport;
import com.young.common.util.*;
import com.young.interfaces.log.annotation.Log;
import com.young.ums.model.RoleUser;
import com.young.ums.service.IRoleUserService;
import com.young.ums.util.AuthHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 角色用户服务
 * Created by imrookie on 2018-10-9.
 */
@Controller
@RequestMapping(value = "/ums/roleUser", method = {RequestMethod.GET, RequestMethod.POST})
public class RoleUserController {

    @Resource(name = "roleUserService")
    IRoleUserService service;

    /**
     * 获取数据列表
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("查询角色用户列表")
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(RoleUser obj, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(obj);//默认权限策略
        List<RoleUser> list = service.getList(obj);
        return Result.buildSuccess(list);
    }

    /**
     * 获取分页数据
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("分页查询角色用户列表")
    @RequestMapping("/getPage")
    @ResponseBody
    public Object getPage(RoleUser obj, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(obj);//默认权限策略
        Page page = CommonUtil.getPageFromRequest(request);
        List<RoleUser> list = service.getPage(obj, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     *
     * @param id
     * @param request
     * @return
     */
    @Log("查询角色用户")
    @RequestMapping("/get")
    @ResponseBody
    public Object get(String id, HttpServletRequest request) {
        RoleUser obj = service.get(id);
        return Result.buildSuccess(obj);
    }

    /**
     * 统计数量
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("统计角色用户数量")
    @RequestMapping("/count")
    @ResponseBody
    public Object count(RoleUser obj, HttpServletRequest request) {
        return Result.buildSuccess(service.count(obj));
    }

    /**
     * 新增
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("新增角色用户")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(RoleUser obj, HttpServletRequest request) {
        service.insert(obj);
        return Result.buildSuccess();
    }

    /**
     * 修改
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("修改角色用户")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(RoleUser obj, HttpServletRequest request) {
        service.update(obj);
        return Result.buildSuccess();
    }

    /**
     * 删除
     *
     * @param ids
     * @param id
     * @param request
     * @return
     */
    @Log("删除角色用户")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) {
        if (StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            service.delete(idArr);
        } else if (StringUtils.isNotBlank(id)) {
            service.delete(id);
        } else {
            return Result.buildFail("删除失败:待删除编号无效!");
        }
        return Result.buildSuccess();
    }

    /**
     * 导入
     *
     * @param file
     * @param request
     * @return
     */
    @Log("导入角色用户")
    @RequestMapping("/import")
    @ResponseBody
    public Object importExcel(MultipartFile file, HttpServletRequest request) {
        try {
            if (file != null && !file.isEmpty()) {
                //excel导入处理,返回excel中的数据集合
                IExcelImport ei = new ExcelImport(file);//将文件转为ExcelImport对象
                //从excel读取数据
                List<RoleUser> list = ei.getImportDataAsBean(RoleUser.class);
                if (list != null && list.size() > 0) {
                    int num = service.batchInsert(list);//批量插入
                    return Result.buildSuccessInfo("成功导入数据" + num + "条!");
                } else {
                    return Result.buildFail("导入失败:excel解析后结果为空!");
                }
            } else {
                return Result.buildFail("导入失败:文件为空!");
            }
        } catch (Exception e) {
            return Result.buildFail("导入失败!");
        }
    }

    /**
     * 导出
     *
     * @param obj      查询参数
     * @param request
     * @param response
     * @throws Exception
     */
    @Log("导出角色用户")
    @RequestMapping("/export")
    public void exportExcel(RoleUser obj, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<RoleUser> list = service.getList(obj);
        if (list != null && list.size() > 0) {
            IExcelExport ee = new ExcelExport();
            ee.insertBeanList(list, RoleUser.class);
            ee.write2Response(response, "excel_" + System.currentTimeMillis() + ".xls");
        } else {
            response.getWriter().write("数据为空!");
        }
    }

    /**
     * 关联角色用户
     *
     * @param roleId
     * @param userIds
     * @param request
     * @return
     */
    @Log("关联角色用户")
    @RequestMapping("/relevanceUsers2Role")
    @ResponseBody
    public Object relevanceUsers2Role(String roleId, String userIds, HttpServletRequest request) {
        if (StringUtils.isBlank(roleId) || StringUtils.isBlank(userIds)) {
            return Result.buildFail("角色ID和用户ID不允许为空");
        }
        service.relevanceUsers2Role(roleId, userIds.split(","));
        return Result.buildSuccess();
    }

    /**
     * 给用户分配角色
     *
     * @param roleIds 角色id集合
     * @param userId  用户id
     * @param request
     * @return
     */
    @Log("给用户追加分配角色")
    @RequestMapping("/relevanceRoles2User")
    @ResponseBody
    public Object relevanceRoles2User(String roleIds, String userId, HttpServletRequest request) {
        AssertUtil.notBlank(roleIds, "角色不允许为空");
        AssertUtil.notBlank(userId, "用户ID不允许为空");

        service.relevanceRoles2User(userId, roleIds.split(","));
        return Result.buildSuccess();
    }

    /**
     * 删除角色用户关系
     *
     * @param roleId  角色id
     * @param userId  用户id
     * @param request
     * @return
     */
    @Log("删除角色用户关系")
    @RequestMapping("/removeRoleUser")
    @ResponseBody
    public Object removeRoleUser(String roleId, String userId, HttpServletRequest request) {
        AssertUtil.notBlank(roleId, "角色不允许为空");
        AssertUtil.notBlank(userId, "用户ID不允许为空");

        service.removeRoleUser(roleId, userId);
        return Result.buildSuccess();
    }

}